package com.example.spring_start.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pojo.article;
import com.example.pojo.banner;
import com.example.req.articleReq;
import com.example.req.articleSaveReq;
import com.example.req.bannerReq;
import com.example.req.bannerSaveReq;
import com.example.resp.PageResp;
import com.example.spring_start.mapper.bannerMapper;
import com.example.utils.CopyUtil;
import com.example.utils.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class bannerServiceImpl extends ServiceImpl<bannerMapper, banner> implements bannerService {
    @Resource
    bannerMapper bannerMapper;
    @Resource
    SnowFlake snowFlake;

    @Override
    public PageResp<banner> getbannerList(bannerReq bannerReq){
        QueryWrapper<banner> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(bannerReq.getBannerusername())){
            queryWrapper.lambda().eq(banner::getBannerusername,bannerReq.getBannerusername());
        }
        if (!ObjectUtils.isEmpty(bannerReq.getBanneradminname())){
            queryWrapper.lambda().eq(banner::getBanneradminname,bannerReq.getBanneradminname());
        }
        Page<banner> page = new Page<>(bannerReq.getPage(),bannerReq.getSize());
        IPage<banner> bannerIPage = bannerMapper.selectPage(page,queryWrapper);
        PageResp<banner> pageResp = new PageResp<>();
        pageResp.setTotal(bannerIPage.getTotal());
        pageResp.setList(bannerIPage.getRecords());
        return pageResp;
    }

    @Override
    public void addbanner(bannerSaveReq req){
        banner pojo = CopyUtil.copy(req, banner.class);
        if (req.getBannerid() == 0){
            pojo.setBannerid(snowFlake.nextId());
            bannerMapper.insert(pojo);
        }else{
            System.out.println(pojo);
            bannerMapper.updateById(pojo);
        }
    }

    @Override
    public void deletebanner(Long bannerid){
        bannerMapper.deleteById(bannerid);
    }
}
